04. SQLAlchemy Object Lifecycle — Part 2

SQLAlchemy Object Lifecycle - Part 2 Heading

SQLAlchemy Object Lifecycle — Part 2

ND004 C01 L04 04 SQLAlchemy Object Lifecycle - Part 2

SQLAlchemy Object Lifecycle - Part 2 Recap

Takeaways

  • A flush takes pending changes, and translates them into commands ready to be committed. It occurs:
  1. when you call Query . Or
  2. on db.session.commit()

A commit leads to persisted changes on the database + lets the db.session start with a new transaction.

When a statement has been flushed already, SQLAlchemy knows not to do the work again of translating actions to SQL statements.

SQLAlchemy Object Lifecycle - Part 2 Quiz

True/False: Commit will automatically flush pending changes for you.

SOLUTION: True

Flush Quiz

Check all the instances when a flush would occur

SOLUTION:
  • `Person.query.all()`
  • `db.session.commit()`
  • `Person.query.filter_by(name='Bob')`
  • `Person.query.first().delete()`